import { usersLogin, usersInfoGetInfoForMy,updatePwdForMy,mobileAuthCode,getCodeMobile } from '@/api/index'
import { ElMessage, ElNotification } from 'element-plus'
import storage from '@/utils/storage'
import router from '@/router'

export default {
    namespaced: true, // 为每个模块添加一个前缀名，保证模块命明不冲突 
    state: () => {
        return {
            userInif: {},
            token: '',
            isPassTimeOut: false,
            isUserLogin: false,
            loginUsername: '',
        }
    },
    mutations: {
        SET_TOKEN(state, token) {
            storage.set('token', token)
            state.token = token
        },
        SET_ISPASSTIMEOUT(state, isPassTimeOut) {
            storage.set('isPassTimeOut', isPassTimeOut)
            state.isPassTimeOut = isPassTimeOut
        },
        SET_ISUSERLOGIN(state, isUserLogin) {
            storage.set('isUserLogin', isUserLogin)
            state.isUserLogin = isUserLogin
        },
        SET_LOGINUSERNAME(state, loginUsername) {
            storage.set('loginUsername', loginUsername)
            state.loginUsername = loginUsername
        },
        USER_INIF(state, userInif) {
            storage.set('userInif', userInif)
            state.userInif = userInif
        }
    },
    actions: {
        usersLogin(context, params) {
            usersLogin(params).then(res => {
                context.commit('SET_TOKEN', res.jwt)
                context.commit('SET_ISPASSTIMEOUT', res.isPassTimeOut)
                context.dispatch('usersInfoGetInfoForMy')
            })
        },
        usersLogout(context, params) {
            // usersLogin(params).then(res => {
                context.commit('SET_TOKEN', '')

            // })
        },
        usersInfoGetInfoForMy(context) {
            usersInfoGetInfoForMy().then(res => {
                context.commit('USER_INIF', res)
                if(context.state.isPassTimeOut==="false"||context.state.isPassTimeOut===false){
                    router.push('/home')
                }else{
                    router.push('/updatePwd')
                }
            })
        },
        updatePwdForMy(context, params) {
            updatePwdForMy(params).then(res => {
                context.commit('SET_ISPASSTIMEOUT', res)
                if(res==="false"||res===false){
                    router.push('/home')
                }else{
                    router.push('/updatePwd')
                }
            })
        },
        mobileAuthCode(context, params) {
            mobileAuthCode(params).then(res => {

                // ElMessage("登录成功-请进行口令验证登录")
                if(res) {
                    context.commit('SET_ISUSERLOGIN', true)
                    context.commit('SET_LOGINUSERNAME', res)
                }
            })
        },
        getCodeMobile(context, params) {
            getCodeMobile(params).then(res => {
                if(res==="操作成功"){
                    ElMessage("发送成功")
                }
            })
        }
    },
}

